// publisher页面 - 文章标题
const form = layui.form;
form.verify({
    articleTitle: [
        /^[\u4E00-\u9FA5a-zA-Z0-9_-]+$/,
        "标题只能是中英文, 数字下划线中划线组成"
    ],
    // 分类判断
    cate: function(){
        return $("select[name=cate_id]").val().length == 0 && "请选择文章类别"
    }
}) 
// 渲染列表
function render() {
    axios({
      method: 'GET',
      url: 'http://api-breakingnews-web.itheima.net/my/article/cates',
      // 自定义请求头
      headers: {
        Authorization: localStorage.getItem('token')
      }
    }).then(({ data: res }) => {
      // console.log(res);
      // 判断请求成功a
      if (res.status === 0) {
        res.data.forEach(obj => {
         const theOpt = `<option value="${obj.Id}" lay-verify="cate">${obj.name}</option> `
         $('select[name=cate_id]').append(theOpt)
        })
        form.render('select','edit')
      }
    })
  }
  render()

  // 初始化富文本编辑器
  initEditor()
  
  let imgUrl = ''

  $('.select').on('click',function () {
     
    $('#file').click()  
  })
  
$('#file').on('change',function (e) {
  const files = e.target.files

  if (files.length === 0) return
  const x = window.URL.createObjectURL(files[0])
  $('#image').prop('src', x)

  imgUrl = files[0]
})


$('form').on('submit',function (e) {
  e.preventDefault()

  const htmlStr = tinyMCE.activeEditor.getContent()

  const fd = new FormData($('form')[0])

  fd.set('content',htmlStr)
  
  fd.append('cover_img',imgUrl)
  fd.forEach((value,key) => {
    console.log("值——属性名",value,key)
  })
  console.log(fd);


  axios({
    method: 'POST',
    url: 'http://api-breakingnews-web.itheima.net/my/article/add',
    // 自定义请求头
    headers: {
      Authorization: localStorage.getItem('token')
    },
    data:fd
  }).then(({ data: res }) => {
    layer.msg(res.message)
    if (res.status === 0) {
      window.parent.$('#wzlist a').click()
      location.href = './../list/list.html'
    }
  })
})









